package com.lagou.zk;

import org.I0Itec.zkclient.ZkClient;

public class Test {
    public static void main(String[] args) throws Exception {

        /*
          创建mysql配置监视器对象，在zk集群上导入初始druid连接池参数，
          同时取得ZkClient对象，方便后续操作
        */
        ZkListen monitor = new ZkListen();
        monitor.connectZk();
        ZkClient zkClient = monitor.getZkClient();

        /*
         从zk进行Mysql配置参数的拉取，并通过mysqlTest()方法测试连接
        */
        Object o = zkClient.readData("/mysql_config");
        System.out.println("读取mysql配置信息，正在获取数据库连接池...");
        DruidUtils.init((String)o);
        System.out.println("此时节点信息为\n" + o);
        System.out.println("正在测试数据库连接。。。");
        DruidUtils.mysqlTest();

        Thread.sleep(6000);

        /*
           更改mysql连接池参数，将要访问的数据库由test1改为test2
        */
        String new_druid_properties = "driverClassName=com.mysql.jdbc.Driver\n" +
                "url=jdbc:mysql://server3:3306/test2?useSSL=false&rewriteBatchedStatements=true\n" +
                "username=hive\n" +
                "password=123456\n" +
                "initialSize=5\n" +
                "maxActive=10\n" +
                "maxWait=3000";
        zkClient.writeData("/mysql_config", new_druid_properties);

        Thread.sleep(6000);

        /*
          从zk上删除mysql配置信息
        */
        zkClient.delete("/mysql_config");

        Thread.sleep(Integer.MAX_VALUE);

    }
}

